﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using PoemGame.Management.Application;
using PoemGame.Management.Domain;
using PoemGame.Management.Repository;
using PoemGame.Query;
using PoemGame.Query.Dapper;

namespace PoemGame.Management.Console
{
    public class Utility
    {
        public static IServiceProvider GetService()
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);

            IConfigurationRoot configuration = builder.Build();
            var services = new ServiceCollection();
            services.AddDbContext<ManagementDbContext>(
                options => options.UseSqlServer(configuration.GetConnectionString("default")));
            services.AddScoped<IDbConnection, SqlConnection>(serviceProvider => {
                SqlConnection conn = new SqlConnection();
                conn.ConnectionString = configuration.GetConnectionString("default");
                return conn;
            });
            services
                .AddScoped<IGameRepository,GameRepository>()
                .AddScoped<IPlayerRepository, PlayerRepository>()
                .AddScoped<IManagementService,ManagementService>()
                .AddScoped<IGameQuery,GameQuery>()
                .AddScoped<IPlayerQuery,PlayerQuery>()
                .AddScoped<IPlayerInGameQuery,PlayerInGameQuery>()
                .AddScoped<IGamePlayRecordQuery,GamePlayRecordQuery>()
                ;
            return services.BuildServiceProvider();
        }
    }
}
